import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/ui/ui.dart';

/// 按钮
class UiButton extends StatelessWidget {
  final Widget child;
  final Function? onPressed;
  final Color backgroundColor;
  final Color? foregroundColor;
  final Color? disabledColor;
  final bool disabled;
  final EdgeInsetsGeometry? padding;
  final BorderRadius? borderRadius;
  final bool outline;
  final bool loading;
  const UiButton({
    super.key,
    required this.child,
    this.onPressed,
    this.backgroundColor = UiTheme.primary,
    this.foregroundColor,
    this.padding,
    this.borderRadius,
    this.disabledColor,
    this.disabled = false,
    this.loading = false,
    this.outline = false,
  });
  bool get buttonStatus => disabled || loading;
  @override
  Widget build(BuildContext context) {
    return outline
        ? OutlinedButton(
            style: OutlinedButton.styleFrom(
              elevation: 0,
              foregroundColor: foregroundColor,
              side: BorderSide(width: .1, color: UiTheme(context).normalColor),
              shape: RoundedRectangleBorder(
                borderRadius: borderRadius ??
                    BorderRadius.circular(
                      UiTheme.boxRadiusX,
                    ),
              ),
            ),
            onPressed: buttonStatus
                ? null
                : () {
                    onPressed?.call();
                  },
            child: buildChild(context),
          )
        : ElevatedButton(
            style: ElevatedButton.styleFrom(
              elevation: 0,
              backgroundColor: backgroundColor,
              foregroundColor: foregroundColor,
              disabledBackgroundColor: backgroundColor.withOpacity(0.6),
              disabledForegroundColor: foregroundColor,
              splashFactory: NoSplash.splashFactory,
              shape: RoundedRectangleBorder(
                borderRadius: borderRadius ??
                    BorderRadius.circular(
                      UiTheme.boxRadiusX,
                    ),
              ),
            ),
            onPressed: buttonStatus
                ? null
                : () {
                    onPressed?.call();
                  },
            child: buildChild(context),
          );
  }

  Widget buildChild(context) {
    return loading
        ? SizedBox(
            height: 20,
            width: 20,
            child: CircularProgressIndicator(
              strokeWidth: 3,
              color: foregroundColor ?? Colors.white,
            ),
          )
        : DefaultTextStyle(
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 15,
              fontFamily: UiTheme.fontFamily,
              color: outline
                  ? UiTheme(context).darkColor
                  : UiTheme(context).lightColor,
            ),
            child: child,
          );
  }
}
